package core.reports;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import core.CoreFacade;
import core.Item;
import core.ItemManager;
import core.Product;
import core.ProductGroup;
import core.ProductManager;
import core.ReportConstants;
import core.StorageUnit;

/**
 * This class will create the Removed Items Report:
 * It will be responsible for getting the necessary data
 * through the visitor pattern, and then constructing the report
 * through the builder pattern.
 * 
 */
public class RemovedItemsGuide implements IDirectorVisitor 
{
	private Calendar fromDate;
	ItemManager itemManager;
	ProductManager productManager;
	
	public RemovedItemsGuide(Calendar fromDate)
	{
		this.fromDate = fromDate;
		itemManager = CoreFacade.getInstance().getIM();
		productManager = CoreFacade.getInstance().getPM();
	}
	
	@Override
	/**
	 * When creating the removed items report this method
	 * will get the necessary information from a productgroup
	 * @param pg the productGroup
	 * @return true if it was successful
	 */
	public boolean visitProductGroup(ProductGroup pg, TraversalOrder order) 
	{
            //method should not be used
            assert(false);
		return false;
	}

	@Override
	/**
	 * When creating the removed items report this method
	 * will get the necessary information from a storage unit
	 * @param su the storage unit
	 * @return true if it was successful
	 */
	public boolean visitStorageUnit(StorageUnit su, TraversalOrder order) 
	{
            //method should not be used
            assert(false);
		return false;
	}


	@Override
	/**
	 * After getting the data from the visit methods, this will
	 * then instruct the appropriate builder how to create the 
	 * necessary report
	 * @return true if it was successful
	 */
	public String construct(String nameOfBuilder) 
	{
		String filename = "";
		 IBuilder builder;
		if(nameOfBuilder.equals(ReportConstants.HTML_BUILDER))
		{
			Date date = new Date();
			builder = new HTMLBuilder("reports\\"+ReportConstants.REMOVED_ITEMS_REPORT + 
					"report" + date.getTime() + ".html"); //add time and relative file path?
			filename = ((HTMLBuilder)builder).getFileName();
		}
		else
		{
			Date date = new Date();
			builder = new PDFBuilder("reports\\"+ReportConstants.REMOVED_ITEMS_REPORT + 
					"report" + date.getTime() + ".pdf");
			filename = ((PDFBuilder)builder).getFileName();
		}
	    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
	    String date = sdf.format(fromDate.getTime());
	    
		builder.addTitle(ReportConstants.REMOVED_ITEMS_TITLE + date);
		builder.addBlankLine();
        
        builder.addTable(ReportConstants.REMOVED_ITEMS_COLUMNS);
        builder.addColumnHeader(ReportConstants.REMOVED_ITEMS_COLUMN1);
        builder.addColumnHeader(ReportConstants.REMOVED_ITEMS_COLUMN2);
        builder.addColumnHeader(ReportConstants.REMOVED_ITEMS_COLUMN3);
        builder.addColumnHeader(ReportConstants.REMOVED_ITEMS_COLUMN4);
        builder.addColumnHeader(ReportConstants.REMOVED_ITEMS_COLUMN5);
        
        List<Item> removedItems = new ArrayList<Item>(itemManager.getRemovedItems());
//        List<Item> allItems = (List<Item>)itemManager.getAllItems();
        List<Product> removedItemProds = new ArrayList<Product>();
        
        Map<Product, Integer> removed = new HashMap<Product,Integer>();
        for(Item item : removedItems)
        {
        	try
			{
				if(item.getExitTime().getTimeInMillis() < fromDate.getTimeInMillis())
					continue;
			} catch (Exception e)
			{
				e.printStackTrace();
			}
        	Product p = item.getProduct();
        	// check if the product was removed
        	if(ProductManager.getInstance().getRemovedProduct(p.getBarcodeString()) != null)
        		continue;
        	if(removed.containsKey(p))
			{
        		removed.put(p, removed.get(p) + 1);
			}
        	else
        	{
        		removed.put(p, 1);
        		removedItemProds.add(p);
        	}
        }
        
        Collections.sort(removedItemProds);
        
        for(Product prod : removedItemProds)
        {
        	int curSupply = itemManager.getItems(prod).size();
        	// Description
        	builder.addCell(prod.getDescription());
        	// Size
        	builder.addCell(prod.getAmount().toString());
        	// Product Barcode
        	builder.addCell(prod.getBarcodeString());
        	// Removed Count
        	builder.addCell(Integer.toString(removed.get(prod)));
        	// Current Supply
        	builder.addCell(Integer.toString(curSupply));
        }
        
        builder.closeCurrentTable();
        builder.finish();
		return filename;
	}
	
}
